package com.zhuxulin.xiaomiapi;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.hl.bean.UserInfo;
import com.hl.util.KL.XmClients.IRequestCallback;
import com.hl.util.KL.XmClients.SendResult;
import com.zhuxulin.xiaomi.api.UserApi;
import com.zhuxulin.xiaomi.api.XMParams;

public class Main extends ActionBarActivity {

	protected static final String TAG = Main.class.getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_main);
		// HashMap<String, Object> params = new HashMap<String, Object>();
		// params.put("UserName", "13547874944");
		// params.put("Password", "123456");
		// params.put("Lat", "0");
		// params.put("Lng", "0");
		
		if (savedInstanceState == null) {
			getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.f_main, container, false);
			rootView.findViewById(R.id.hello).setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					try {
						UserApi.getApi(getActivity(),null).apiUsersLogin(
								new XMParams.Builder()
								.addParam("UserName", "13547874944")
								.addParam("Password", "123456")
								.addParam("Lat", "0")
								.addParam("Lng", "0")
								.create(), new IRequestCallback() {
									@Override
									public void Callback(SendResult<?> result) {
										@SuppressWarnings("unchecked")
										SendResult<UserInfo> sendResult = (SendResult<UserInfo>) result;
										Log.i(TAG, sendResult.toString());
										Log.i(TAG, sendResult.getData().toString());
									}
								});
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
			return rootView;
		}
	}

}
